package ui;

import domain.User;

import javax.sound.sampled.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static Utils.ReadUsers.readUsers;

/**
 * 登录界面
 */
public class LoginJFrame extends JFrame {
    //存储注册用户集合
    static List<User> users=new ArrayList<User>();
    static {
        try {
            users = readUsers();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    //标题
    JLabel titleLabel = new JLabel("登录人生重开模拟器");
    //登录按钮
    JButton login = new JButton("登录");
    //忘记密码按钮
    JButton forgetPassword = new JButton("忘记密码");
    //没有账号？请
    JLabel noAccount = new JLabel("没有账号？请");
    //去注册
    JButton goToRegister = new JButton("注册");
    //请输入账号
    JLabel usernameText = new JLabel("请输入账号");
    //用户名文本框
    JTextField username = new JTextField();
    //请输入密码
    JLabel passwordText = new JLabel("请输入密码");
    //密码文本框
    JPasswordField password = new JPasswordField();

    public LoginJFrame() {
        //初始化界面
        initJFrame();
        //在这个界面中添加内容
        initView();

        //添加图片
        initImage();
        //是否可见(写在最后)
        this.setVisible(true);
    }
    private void initImage() {
        //添加背景图片
        JLabel background = new JLabel(new ImageIcon(this.getClass().getResource("/image/RegisterJFrame/背景.png")));
        background.setBounds(0, 0, 603, 680);
        this.getContentPane().add(background);
    }
    /**
     *负责人:X+Y=Z
     * 功能:添加"请输入账号","请输入密码,并为其添加文本输入框
     * 添加登录,注册按钮
     * 点击对应按钮执行对应逻辑
     * 如登录成功弹出一个"登录成功"弹窗
     * 账号或密码匹配不上弹出一个"账号或密码错误"弹窗
     */
    private void initView() {
        //标题
        titleLabel.setFont(new Font("楷体", Font.PLAIN, 30));
        titleLabel.setBounds(165, 120, 500, 50);
        this.add(titleLabel);

        //登录按钮
        login.setBounds(150, 500, 100, 40);
        login.setFont(new Font("楷体", Font.PLAIN, 20));
        login.setBackground(new Color(253, 222, 122));
        this.add(login);

        //忘记密码按钮
        forgetPassword.setBounds(300,500,128,40);
        forgetPassword.setBackground(new Color(253, 222, 122));
        forgetPassword.setFont(new Font("楷体", Font.PLAIN, 20));
        this.add(forgetPassword);

        //没有账号？请
        noAccount.setBounds(300, 350, 200, 50);
        noAccount.setFont(new Font("楷体", Font.PLAIN, 20));
        this.add(noAccount);

        //去注册
        goToRegister.setBounds(450, 350, 100, 40);
        goToRegister.setBackground(new Color(253, 222, 122));
        goToRegister.setFont(new Font("楷体", Font.PLAIN, 20));
        this.add(goToRegister);

        //请输入账号
        usernameText.setBounds(150, 200, 100, 50);
        usernameText.setFont(new Font("楷体", Font.PLAIN, 20));
        this.add(usernameText);

        //用户名文本框
        username.setBounds(270, 210, 200, 30);
        this.add(username);

        //请输入密码
        passwordText.setBounds(150, 250, 100, 50);
        passwordText.setFont(new Font("楷体", Font.PLAIN, 20));
        this.add(passwordText);

        //密码文本框
        password.setBounds(270, 260, 200, 30);
        this.add(password);



        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Utils.Music.playSound("src\\music\\02.wav");
                String inputUsername = username.getText();
                String inputPassword = new String(password.getPassword());
                boolean isMatched = false;
                for (User user : users) {
                    if (user.getUsername().equals(inputUsername) && user.getPassword().equals(inputPassword)) {
                        isMatched = true;
                        break;
                    }
                }
                if (isMatched) {
                    JOptionPane.showMessageDialog(LoginJFrame.this, "登录成功");
                    new GameMenuJFrame();
                    dispose();
                } else {
                    JOptionPane.showMessageDialog(LoginJFrame.this, "账号或密码错误");
                }
            }
        });

        // 忘记密码按钮点击事件
        forgetPassword.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Utils.Music.playSound("src\\music\\02.wav");
//                JOptionPane.showMessageDialog(LoginJFrame.this, "找回密码");
                new ForgetPasswordJFrame();
                dispose();
            }
        });

        // 去注册按钮点击
        goToRegister.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Utils.Music.playSound("src\\music\\02.wav");
//                JOptionPane.showMessageDialog(LoginJFrame.this, "即将跳转到注册页面");
                new RegisterJFrame();
                dispose();
            }
        });
    }

    private void initJFrame() {
        //设置长宽(像素)
        this.setSize(603, 680);
        //设置界面的标题
        this.setTitle("人生重开模拟器 v1.0");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //取消默认的居中方式,只有取消了才会按照,x y轴的方式添加组件
        this.setLayout(null);
        //给整个界面添加键盘监听
        //this.addKeyListener(this);
    }
}


